import pygame
from pygame.locals import *
import pygame.sprite
print "unidades.robots.robots_defensa cargado satisfactoriamente"

class RobotDefensivoComun(pygame.sprite.Sprite):
    def __init__(self, cuadro, linea, balasGrupo):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.image.load("images/torre1.png").convert_alpha()
        self.rect = self.image.get_rect()
        self.cuadro = cuadro
        self.rect.centerx = cuadro.x
        self.rect.centery = cuadro.y
        self.vida = 500
        self.atacado = False
        self.frames = 0
        self.linea = linea
        self.balasRobotGrupo = balasGrupo
    def update(self):
        self.frames += 1
        if self.frames % 100 > 50:
            self.image = pygame.image.load("images/torre2.png").convert_alpha()
        else:
            if self.frames == 100:
                self.frames = 0
            self.image = pygame.image.load("images/torre1.png").convert_alpha()
        self.atacar()
        self.verifica_vida()
    def atacar(self):
        for atacante in self.linea.unidades:
            if atacante.rect.centerx > self.rect.centerx and self.frames % 150 == 1:
                self.balasRobotGrupo.add(BalaRobotComun(self.rect.center))
                break
    def recibir_ataque(self, velocidad_ataque,value):
        if self.frames % velocidad_ataque == 0:
            self.vida -= value
    def verifica_vida(self):
        if self.vida <= 0:
            self.cuadro.unidad = None
            self.kill()

class BalaRobotComun(pygame.sprite.Sprite):
    def __init__(self,pos):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.image.load("images/bala.png")
        self.rect = self.image.get_rect()
        self.rect.midright = pos
        self.ataque = 10
        self.mask = pygame.mask.from_surface(self.image)
    def update(self):
        if self.rect.right > 1000:
            self.kill()
        else:
            self.rect.move_ip(4,0)